/*
 * $Id: SiebelRequestHandler.java,v 1.4 2013/02/07 20:05:55 l501902876 Exp $
 * 
 * Copyright (C) 2010 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.createrfs;

import com.ge.healthcare.autosc.common.createrfs.RFSLogger;
import com.ge.healthcare.autosc.createrfs.request.CreateRFSRequest;
import com.ge.healthcare.autosc.createrfs.response.CreateRFSResponse;
import com.ge.med.service.eds.commons.DataException;
import com.ge.med.service.eds.commons.ResourceException;
import com.ge.med.service.eds.rfs.IRFS;
import com.ge.med.service.eds.rfs.ObjectFactory;
import com.ge.med.service.eds.rfs.vo.ServiceCreateRequest;
import com.ge.med.service.eds.rfs.vo.ServiceCreateResponse;

/**
 * Handler for CreateRFSRequest messages.
 * 
 */
public class SiebelRequestHandler {
	private static final String FAILED = "FAILED";
	private static final String OK = "OK";
	private static final String CONTACT_FIRST_NAME="Proactive Services";
	private static final String CONTACT_LAST_NAME="Proactive Services";

	public CreateRFSResponse handle(Object object) {
		CreateRFSResponse resp = null;

		CreateRFSRequest request = (CreateRFSRequest) object;
		try {
			ServiceCreateRequest siebelVO = new ServiceCreateRequest();
			IRFS iRFS = ObjectFactory.getInstance().getRFSObject();

			// set the values required in the siebel request object
			siebelVO.setAssetId(request.getAssetInfo().getAssetId());
			siebelVO.setCRM(request.getAssetInfo().getCrm());
			siebelVO.setSource("Asset-Initiated");// Customer-Initiated,Indicates
													// that SR was initiated due
													// to customer request,
													// automated monitoring etc.
			siebelVO.setSubSource("PPADS");
			siebelVO.setContactFirstName(CONTACT_FIRST_NAME);// First Name
			siebelVO.setContactLastName(CONTACT_LAST_NAME);// Last Name
			siebelVO.setType("Corrective Repair");// Corrective Repair,Parts
													// Sales
			siebelVO.setSubtype("MON");
			siebelVO.setDescription(request.getSymptom1() + " "
					+ request.getSymptom2());
			RFSLogger.info(this.getClass(), "handle", "Request:" + siebelVO);
			// create the siebel request
			ServiceCreateResponse responseVO = (ServiceCreateResponse) iRFS
					.create(siebelVO);
			// create an OK response in case the siebel request succeeds, else
			// create a FAILED response in case of exceptions
			resp = new CreateRFSResponse(responseVO.getSRId(), null, OK,
					responseVO.getResponseMessage());
			RFSLogger.info(this.getClass(), "handle",
					String.format("Received response: %s", responseVO));
		} catch (ResourceException e) {
			// e.printStackTrace();
			RFSLogger.fatal(this.getClass(),
					"resource exception caught in handle", e);
			resp = new CreateRFSResponse(null, null, FAILED, e.getMessage());
		} catch (DataException e) {
			// e.printStackTrace();
			RFSLogger.fatal(this.getClass(), "data exception caught in handle",
					e);
			resp = new CreateRFSResponse(null, null, FAILED, e.getMessage());
		} catch (Exception e) {
			// e.printStackTrace();
			RFSLogger.fatal(this.getClass(), "handle", e);
			resp = new CreateRFSResponse(null, null, FAILED, e.getMessage());
		}
		return resp;
	}
}
